package com.example.administrator.mysmsall;

import android.Manifest;
import android.content.ContentResolver;
import android.content.pm.PackageManager;
import android.database.Cursor;
import android.net.Uri;
import android.support.v4.app.ActivityCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.google.gson.Gson;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class MainActivity extends AppCompatActivity {
    public static final int REQUEST_PERMISSION_CODE = 1;
    private TextView tv ;
    private EditText et ;
    private List list = new ArrayList();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        et= findViewById(R.id.et);
        tv = findViewById(R.id.tv);
        tv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                obtainPhoneMessage();
            }
        });
        if (ActivityCompat.checkSelfPermission(this, Manifest.permission.READ_SMS)

                != PackageManager.PERMISSION_GRANTED) {

            ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.READ_SMS}, REQUEST_PERMISSION_CODE);
        }
    }

    private Uri SMS_INBOX = Uri.parse("content://sms/");

    private void obtainPhoneMessage() {
        ContentResolver cr = getContentResolver();
        String[] projection = new String[]{"_id", "address", "person", "body", "date", "type"};
        Cursor cur = cr.query(SMS_INBOX, projection, null, null, "date desc");
        if (null == cur) {
            Log.i("ooc", "************cur == null");
            return;
        }
        if (cur != null) {
            Log.i("ooc", "cursor length is " + cur.getCount());
            try {
                while (cur.moveToNext()) {
                    String smsDate = cur.getString(cur.getColumnIndex("date"));
                    SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
                    Date d = new Date(Long.parseLong(smsDate));
                     smsDate = dateFormat.format(d);
                    /*
                    原文：https://blog.csdn.net/gyhgx/article/details/51511091
               */

                    String number = cur.getString(cur.getColumnIndex("address"));//发件人手机号
                    String name = cur.getString(cur.getColumnIndex("person"));//联系人姓名列表
                    String body = cur.getString(cur.getColumnIndex("body"));//短信内容
                    //至此就获得了短信的相关的内容, 以下是把短信加入map中，构建listview,非必要。
                    Map<String, Object> map = new HashMap<String, Object>();
                    map.put("num", number);
                    map.put("mess", body);
                    list.add(map);
                }

                Gson gson = new Gson();
                String json=gson.toJson(list);
                Log.e("json",json);

                et.setText(cur.getCount()+json);
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                cur.close();  //关闭cursor，避免内存泄露
            }
        }
    }
}
/*---------------------
    作者：LXB-89
    来源：CSDN
    原文：https://blog.csdn.net/adminlxb89/article/details/81068419
    版权声明：本文为博主原创文章，转载请附上博文链接！*/


//    String number = cur.getString(cur.getColumnIndex("想获得的属性")); //获取方法
/**
 * sms主要结构：
 *  _id：短信序号，如100
 *  thread_id：对话的序号，如100，与同一个手机号互发的短信，其序号是相同的
 *  address：发件人地址，即手机号，如+8613811810000
 *  person：发件人，如果发件人在通讯录中则为具体姓名，陌生人为null
 *  date：日期，long型，如1256539465022，可以对日期显示格式进行设置
 *  protocol：协议0SMS_RPOTO短信，1MMS_PROTO彩信
 *  read：是否阅读0未读，1已读
 *  status：短信状态-1接收，0complete,64pending,128failed
 *  type：短信类型1是接收到的，2是已发出
 *  body：短信具体内容
 *  service_center：短信服务中心号码编号，如+8613800755500

*/

